#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
struct book
{
	char bname[10];
	int bno;
	int bmoney;
	char bbookhome[20];
	char bwhere[50];
	int bphone;
	int bmumber;
};
struct student
{
	int sclass;
	char sname[12];
	int sno;
	int sphone;
	char swhere[50];
	book b;
};
struct school
{
	book have[100];
	book no[100];
};
void menu(student a[1000],school *d,int *i,int *h,int *g);
void menu1(student a[1000],int *i);
void menu2(student a[1000],school *d,int *i,int *h,int *g);
void menu4(student a[1000],school *d,int *i,int *h,int *g);
void menu3(school *d,int *h,int *g);

int main()
{
	student a[1000];
	school d;
	int i=0;
	int h=0;                                        //没有的书
	int g=0;                                        //有的书
	menu(a,&d,&i,&h,&g);
	return 0;
}

void menu(student a[1000],school *d,int *i,int *h,int *g){
while(1){
		system("cls");
		printf("\t\t--------------------------大学教材购销管理系统--------------------------------\n");
		printf("\t\t-------------------------------1.学生用书系统---------------------------------\n");
		printf("\t\t-------------------------------2.购书系统-------------------------------------\n");
		printf("\t\t-------------------------------3.补售系统-------------------------------------\n");
		printf("\t\t-------------------------------0.退出-----------------------------------------\n");
		printf("\t\t-------------------------------输入序号:");
		int op;
		scanf("%d",&op);
		switch(op)
		{
			case 1:{
				menu1(a,i);
				break;
				//menu()
			}
			case 2:{	
			   menu2(a,d,i,h,g);	
				//menu2();
				break;
			}
			case 3:{
			   menu3(d,h,g);
				//menu3()
			}
			
			case 0:{
				return;
				break;
			}	
			default:continue;
			
		}
		                         //pause暂停
		
	}

}
void menu1(student a[1000],int *i){
	system("cls");
				printf("\t\t--------------------录入数据---------------\n");
				printf("\t\t-----------------请输入学生信息------------\n");
				printf("姓名：");
				scanf("%s",&a[(*i)].sname);
				printf("学号：");
				scanf("%d",&a[(*i)].sno);
				printf("班级：");
				scanf("%d",&a[(*i)].sclass);
				printf("电话：");
				scanf("%d",&a[(*i)].sphone);
				printf("地址：");
				scanf("%s",&a[(*i)].swhere);
				printf("\t\t---------------请输入该学生的用书信息------------\n");
				printf("书名：");
				scanf("%s",&a[(*i)].b.bname);
				fflush(stdin);
				printf("书号：");
				scanf("%d",&a[(*i)].b.bno);
				fflush(stdin);
				printf("价格：");
				scanf("%d",&a[(*i)].b.bmoney);
				printf("出版社：");
				scanf("%s",&a[(*i)].b.bbookhome);
				printf("地址：");
				scanf("%s",&a[(*i)].b.bwhere);
				printf("电话：");
				scanf("%d",&a[(*i)].b.bphone);
				printf("输入完成！");
				
				i++;
				//menu1();
	return;
}
void menu2(student a[1000],school *d,int *i,int *h,int *g)
{
	while(1)
	{
		system("cls");
		printf("\t\t---------------学生购书系统----------------\n");
		printf("\t\t---------------1.身份核对------------------\n");
		printf("\t\t---------------0.退出----------------------\n");
		printf("\t\t---------------输入序号：");
		int lo=0;
		scanf("%d",&lo);
		switch(lo)
		{
			case 1:{
				menu4(a,d,i,h,g);
				break;
			}
			case 0:{
				return;
			}
			default:continue;
		}
		
	}
}
void menu4(student a[1000],school *d,int *i,int *h,int *g){
	while(1)
	{
		system("cls");
		
	
				printf("\t\t----------欢迎来到购书系统--------\n");
				int ol=0;
				printf("请输入正确的学生信息：");
				student c;
				printf("姓名：");
				int p;
				scanf("%s",&c.sname);
				fflush(stdin);
				printf("学号：");
				scanf("%d",&c.sno);
				fflush(stdin);
				printf("班级：");
				scanf("%d",&c.sclass);
				fflush(stdin);
				printf("电话：");
				scanf("%d",&c.sphone);
				fflush(stdin);
				printf("地址：");
				scanf("%s",&c.swhere);
				
				for(int j = 0;j < (*i);j ++)
				{
					if(c.sno == a[j].sno&&c.sclass == a[j].sclass)
					{
						ol=1;	
					}
				 	if(ol==1)
					{
						printf("输入正确，前往购书\n");	
						system("cls");
						printf("请输入要购买的书名和书号");
					   	scanf("%s%d",&c.b.bname,&c.b.bno);
					   	fflush(stdin);
						if(strcmp(c.b.bname,a[j].b.bname)==0&&c.b.bno==a[j].b.bno)
							{
								ol=2;
							}
							else
							{
								printf("你不可购买该书籍");
								system("pause");
								break;
							}
				
	            	}
				}
				if(ol==2)
				{
					for(int j=0;j<(*g);j++)
					{
						if(strcmp(d->have[j].bname,c.b.bname)==0)
						{
							printf("购买成功");
							ol=3;
							d->have[j].bmumber--;                          //删书籍数量
							for(int o=j;o<(*g);o++)
							{
								if(d->have[o].bmumber==0)
								{	
									d->have[o].bno=d->have[o+1].bno;
								    strcpy(d->have[o].bname,d->have[o+1].bname);
								    (*g)--;		
								}
							}
						}
					}
					if(ol!=3)
					{
						
						printf("库存不足，请下次购买");
						system("pause");
						for(int t=0;t<(*h);t++)
						{
							if(strcmp(d->no[t].bname,c.b.bname)==0&&d->no[t].bno==c.b.bno)
							{
								d->no[t].bmumber=d->no[t].bmumber+1;
								p=9;
							}
					
						}
						if(p!=9){
						
							d->no[(*h)].bmumber=1;
							strcpy(d->no[(*h)].bname,c.b.bname);
							d->no[(*h)].bno=c.b.bno;
							(*h)++;
						}
					}
				}
				if(ol==0){
					printf("输入错误，请检查信息\n");
					system("pause");
					return;
				}
				return;
			}
	}

void menu3(school *d,int *h,int *g)
{
	while(1)
	{
		system("cls");
				printf("\t\t-----------------教材补售管理系统----------------\n");
				printf("\t\t-------------------1.查询缺书信息----------------\n");
				printf("\t\t-------------------2.购买书籍--------------------\n");
				printf("\t\t-------------------0.退出------------------------\n");
				int on;
				scanf("%d",&on);
			    switch(on)
			    {
			    	case 1:{
			    		system("cls");
			    		printf("\t\t-------------缺书查询---------------\n");
			    		if((*h)==0)
			    		{
			    			printf("\t\t无缺少书籍\n");
							system("pause");
						}
						else{
						
			    			for(int j = 0;j < (*h);j ++)
			    			{
			    				printf("书名为%s\n",d->no[j].bname);
			    				printf("书号为%d\n",d->no[j].bno);
			    				printf("该本书缺%d\n本",d->no[j].bmumber);
			    				system("pause");
							}
						}
						
			    	
						break;
					}
					case 2:
					{
						while(1)
						{
						
					    system("cls");
					    printf("\t\t----------购书-------------------\n");
					    printf("书名为\n");
					    scanf("%s",&d->have[(*g)].bname);
			    		printf("书号为\n");
			    		scanf("%d",&d->have[(*g)].bno);
			    		printf("输入购买数量");
			    		scanf("%d",&d->have[(*g)].bmumber);
			    		printf("购买完成");
			    		system("pause");
			    		(*g)++;
			    		for(int j=0;j<(*g);j++)
			    			{
			    				for(int k=0;k<(*h);k++)
			    				{
			    					if(strcmp(d->have[j].bname,d->no[k].bname)==0&&d->have[j].bno==d->no[k].bno&&d->have[j].bmumber>=d->no[k].bmumber)
			    					{
			    				    	for(int o=k;o<j;o++)
			    				    	{
			    						d->no[o].bno=d->no[o+1].bno;
			    						strcpy(d->no[o].bname,d->no[o+1].bname);
			    				    	}
			    				    	h--;
									}
									else if(strcmp(d->have[j].bname,d->no[k].bname)==0&&d->have[j].bno==d->no[k].bno&&d->have[j].bmumber<d->no[k].bmumber)
									{
										d->no[k].bmumber=d->no[k].bmumber-d->have[j].bmumber;
									}
								}
							}
							break;
						}
					}
					case 0:return;
					default :continue;
				}
					
	}
}
	
	
	
